﻿/******************************************************/
// RegisterPage プレゼンテーションクラス  
/******************************************************/

using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using MAA.Business;
using MAA.Entity;

namespace MAA.Presentation.PRL.Account
{
    public partial class RegisterPage : System.Web.UI.Page
    {
        private ResourceManager rm = new ResourceManager("MAA.Presentation.language", System.Reflection.Assembly.GetExecutingAssembly());
        private CultureInfo m_JapanCulture = new CultureInfo("ja-JP");
        private CultureInfo m_VietnameCulture = new CultureInfo("vi");

        private void UpdateUI()
        {
            RequiredFieldValidator2.Text = rm.GetString("langEnterPass");
            langRegisterAcc.Text = rm.GetString("langRegisterAcc");
            langName.Text = rm.GetString("langName");
            RequiredFieldValidator1.Text = rm.GetString("langErrNameEntity");
            RequiredFieldValidator3.Text = rm.GetString("langErrEmailEntity");
            RegularExpressionValidator1.Text = rm.GetString("langErrEmail");
            langPass.Text = rm.GetString("langPass");
            RegularExpressionValidator2.Text = rm.GetString("langPassErr");
            langPasslonger6.Text = rm.GetString("langPasslonger6");
            langConfirmMail.Text = rm.GetString("langConfirmMail");
            RequiredFieldValidator4.Text = rm.GetString("langErrConfirmMailEntity");
            CompareValidator1.Text = rm.GetString("langErrConfirmMail");
            langAddress.Text = rm.GetString("langAddress");
            btnOK.Text = rm.GetString("langRegister");
            btnCancel.Text = rm.GetString("langCancel");
            langEmail.Text = rm.GetString("langEmail");
        }

        /// <summary>
        /// Load page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["language"] == null)
            {
                Session["language"] = "vi";
            }
            if (Session["language"].Equals("vi"))
            {
                Thread.CurrentThread.CurrentUICulture = m_VietnameCulture;
                UpdateUI();
            }
            if (Session["language"].Equals("jp"))
            {
                Thread.CurrentThread.CurrentUICulture = m_JapanCulture;
                UpdateUI();
            }
            if (this.IsPostBack == true) return;

            // View name of all provinces in DropDownList Province
            BOLNews bolNews = new BOLNews();
            DataTable tblProvince = new DataTable();
            string messageProvince = "";
            string lang = Session["language"].ToString();
            if (!bolNews.viewProvince(lang,out tblProvince, out messageProvince))
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                    "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(messageProvince) + "')</script>", false);
            }
            ddlProvince.DataSource = tblProvince;
            ddlProvince.DataValueField = "ID";
            ddlProvince.DataTextField = "Name";
            ddlProvince.DataBind();
        }

        /// <summary>
        /// On click Button Register
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            // Get information of user in order to register
            User user = new User();
            user.Email = tbxEmail.Text.Trim();
            user.Password = tbxPassword.Text.Trim();
            user.Password = PRLCommon.Encrypt(user.Password, true);
            user.Name = tbxName.Text.Trim();
            user.Address = tbxAddress.Text.Trim();
            user.ProvinceID = Convert.ToInt16(ddlProvince.SelectedItem.Value);

            // Perform register
            string messageRegister = "";
            BOLAccount bolAcc = new BOLAccount();
            if (bolAcc.register(user, out messageRegister))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "popup",
                    "<script type='text/javascript'>alert('" + PRLCommon.ChangeLanguageMessage(messageRegister) + "');window.location='../HomePage.aspx'</script>");
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                    "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(messageRegister) + "')</script>", false);
            }
        }

        /// <summary>
        /// On click Button Cancel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/HomePage.aspx");
        }
    }
}